import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
let timestamp = Date.parse(new Date()) / 1000;
let wxCharts = require('../../utils/wxcharts.js');
let globalData = getApp().globalData;
let radarChart = null;
let columnChart = null;
let ringChart = null;
Page({
    ...HttpUtils.func,
    data: {
        toplist: ['综合', '自学', '学分', '任务', '会议', '考试'],
        currentIndex: 0,
        report: {}
    },
    onLoad() {
         let url = globalData.dangjianUrl + '/member/partyReport',
          // let url = 'http://47.98.56.45:9001/public/api/member/partyReport',
            params = {
                'partyId': globalData.partyId,
                'userId': globalData.userId
            },
            param = JSON.stringify(params);

        this.httpGet(url, param, '支部简介加载失败').then(
            (respData) => {
                let result = respData.data.result && respData.data.result.code;
                if (+result == 1) {
                    let res = respData.data;
                    this.showSpider(res); 
                    console.log(res);
                } else {
                    this.showPageMsg('alert', respData.data.result.description);
                }
            }
        );
        this.getUsrInfo(); 
    },
    getUsrInfo() {
        let newUrl = getApp().globalData.dangjianUrl + "/member/memberInfo";
        let params = {};
        params['userId'] = getApp().globalData.userId + "";
        params = JSON.stringify(params);
        this.httpGet(newUrl, params, "人员基本信息查询失败").then(
            (respData) => {
                var result = respData.data.result.code;
                if (result == 1) {
                    let member = respData.data.member;
                    this.setData({
                        "member": member
                    });
                } else {
                    this.showPageMsg('alert', respData.data.result.description)
                }
            }
        );
    },
    changeInfo(e) {
        let that = this,
            i = e.currentTarget.dataset.index;

            wx.showLoading({
                'mask': !0
            });
            that.setData({
                'currentIndex': i
            });
            switch(+i) {
                case 0:
                    that.showSpider();
                    break;
                case 1:
                    that.showColumn(0);
                    break;  
                case 2:
                    that.showRing(0);
                    break;  
                case 3:
                    that.showRing(1);
                    break; 
                case 4:
                    that.showRing(2);
                    break; 
                case 5:
                    that.showExam();
                    break;  
                default:
                    return false;
            }
    },
    showSpider(data) {
        let that = this,
            report = data.report,
            stars = report.stars,
            windowWidth = 320;

        try {
            var res = wx.getSystemInfoSync();
            windowWidth = res.windowWidth;
        } catch (e) {
            console.error('getSystemInfoSync failed!');
        }
        that.setData({
            'report': report
        })
        // spider
        radarChart = new wxCharts({
            canvasId: 'radarCanvas',
            type: 'radar',
            categories: ['自主性', '学习量', '关注度', '活跃度', '参与度'],
            series: [{
                name: '成交量1',
                data: [stars.ability, stars.volume, stars.attent, stars.activity, stars.degree]
            }],
            width: windowWidth,
            height: 250,
            dataLabel: !0,
            legend: !1,
            dataPointShape: !1,
            extra: {
                radar: {
                    max: 20
                }
            }
        });

        setTimeout(()=>{
           wx.hideLoading();
        }, 800);
    },
    showColumn(type) {
        let that = this, 
            columnChart = [
            {
                type: 'columnCanvas',
                main: {
                    title: '总成交量',
                    data: [23, 28, 35, 54, 95],
                    categories: ['10月', '11月', '12月', '1月', '当月'],
                    units: '天'
                }
            }, {
                type: 'columnCanvasTime',
                main: {
                    title: '总成交量',
                    data: [23, 28, 35, 54, 95],
                    categories: ['10月', '11月', '12月', '1月', '当月'],
                    units: '小时'
                }
            }, {
                type: 'columnCanvasMonth',
                main: {
                    title: '总成交量',
                    data: [23, 28, 35, 54, 95],
                    categories: ['10月', '11月', '12月', '1月', '当月'],
                    units: '分'
                }
            }];
        // column
        if (type <= 0) {
            for(let i = 0 ; i < 2; i++) {
                that.drawColum(columnChart[i]);
            }
        } else {
            that.drawColum(columnChart[2]);
        }
    },
    drawColum(obj) {
        let windowWidth = 320, columnChart = obj.type;

        try {
            var res = wx.getSystemInfoSync();
            windowWidth = res.windowWidth;
        } catch (e) {
            console.error('getSystemInfoSync failed!');
        }
        columnChart = new wxCharts({
            canvasId: obj.type,
            type: 'column',
            animation: true,
            categories: obj.main.categories,
            legend: !1,
            series: [{
                name: '成交量',
                data: obj.main.data,
                color: '#3eaff7'
            }],
            yAxis: {
                format: function (val) {
                    return val + obj.main.units;
                },
                min: 0
            },
            xAxis: {
                disableGrid: true,
                type: 'calibration'
            },
            extra: {
                column: {
                    width: 15
                }
            },
            width: windowWidth,
            height: 200,
        });
        setTimeout(() => {
            wx.hideLoading();
        }, 800);
    },
    showRing(type) {        
        let id = ['ringCanvas', 'ringCanvasMission', 'ringCanvasMeeting', 'ringCanvasExam'],ringChart = ['ring1', 'ring2', 'ring3', 'ring4'];
        
        ringChart[id] = new wxCharts({
            animation: true,
            canvasId: id[type],
            type: 'ring',
            extra: {
                ringWidth: 20,
                pie: {
                    offsetAngle: -45
                }
            },
            title: {
                name: '180',
                color: '#333333',
                fontSize: 28
            },
            series: [{
                name: '成交量1',
                data: 90,
                color: '#3eaff7',
                stroke: false
            }, {
                name: '成交量2',
                data: 90,
                color: '#4fdad2',
                stroke: false
            }],
            disablePieStroke: true,
            width: 250,
            height: 200,
            dataLabel: false,
            legend: false,
            padding: 0
        });
        ringChart[id].addEventListener('renderComplete', () => {
            console.log('renderComplete');
        });
        setTimeout(() => {
            ringChart[id].stopAnimation();
            wx.hideLoading();
        }, 500);
    },
    showExam() {
        let that = this;

            that.showRing(3);
            that.showColumn(1);
    }
});